<template>
</template>

<script>
	import api from '../../kits/api.js';
	export default{
		name:"Dtal",
		data(){
			return{}
		},
		created(){
			this.authent()
		},
		methods:{
			authent(){
				var _config;
				var me = this
				var url = Host +'/api/dt/jsconfig/pc'; //pc端钉钉鉴权
				$.ajax({
					url: url,
					type: "get",
					data: "",
					dataType: "json",
					async: false,
					contentType: "application/json",
					success: function(res) {
						console.log(res)
						_config = {
							corpId: res.data.corpId,
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							signature: res.data.signature,
							agentId: res.data.agentId,
							rebackUrl: res.data.url,
							jsticket: res.data.jsticket
						};
	
						DingTalkPC.config({
							agentId: _config.agentId,
							corpId: _config.corpId,
							timeStamp: _config.timeStamp,
							nonceStr: _config.nonceStr,
							signature: _config.signature,
							jsApiList: [
								"runtime.info",
								"biz.contact.choose",
								"device.notification.confirm",
								"device.notification.alert",
								"device.notification.prompt",
								"biz.ding.post",
								"biz.user.get",
								"biz.util.openLink"
							]
						});
						DingTalkPC.ready(function() {
							console.log(1)
							DingTalkPC.biz.user.get({
									onSuccess: function(info) {
										console.log("openid:", info.id)
										$.ajax({
											url: Host +'/api/account/openid/'  + info.id ,
											type: "get",
											data: "",
											dataType: "json",
											async: false,
											contentType: "application/json",
											success: function(res) {
												if(res.isOK){
													localStorage.login = res.data.Login;
													localStorage.token = res.data.Token;
													me.$router.push({
														path: '/index'
													})
												}else{				
													me.$router.push({
														path: '/login'
													})
													localStorage.openid=info.id;
												}
											},
											error: function(res) {
												me.$router.push({
														path: '/login'
												})
	                                    		localStorage.openid = info.id 
											}
										})//window.location.href = "/?openid=" + info.id 
									},
									onFail: function(err) {
										alert("dd error: " + JSON.stringify(err));
			                            me.$router.push({
											path: '/login'
										})
			                            logger.e('userGet fail: ' + JSON.stringify(err));
									}
							});
						});
	
						DingTalkPC.error(function(err) {
							me.$router.push({
								path: '/login'
							}) //登陆页
							alert("dd error: " + JSON.stringify(err));
	                    	console.log("钉钉鉴权失败")
						});
						
						me.$router.push({
							path: '/login'
						})
					},
					error: function(error) {
						alert(error)
						me.$router.push({
							path: '/login'
						}) //登陆页
						console.log(error);
					}
				})
			}
		}
	}
</script>
<style>
</style>
			